package com.charm.restaurant.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.charm.restaurant.common.Result;
import com.charm.restaurant.entity.Dish;
import com.charm.restaurant.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品控制器
 */
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 新增菜品
     */
    @PostMapping
    public Result<Dish> save(@RequestBody Dish dish) {
        log.info("新增菜品：{}", dish.getName());
        return dishService.saveDish(dish);
    }

    /**
     * 分页查询菜品
     */
    @GetMapping("/page")
    public Result<Page<Dish>> page(int page, int pageSize, String name) {
        log.info("分页查询菜品，page={}, pageSize={}, name={}", page, pageSize, name);
        return dishService.pageDish(page, pageSize, name);
    }

    /**
     * 根据ID查询菜品
     */
    @GetMapping("/{id}")
    public Result<Dish> getById(@PathVariable Long id) {
        log.info("根据ID查询菜品：{}", id);
        return dishService.getDishById(id);
    }

    /**
     * 修改菜品
     */
    @PutMapping
    public Result<Dish> update(@RequestBody Dish dish) {
        log.info("修改菜品：{}", dish.getName());
        return dishService.updateDish(dish);
    }

    /**
     * 删除菜品
     */
    @DeleteMapping
    public Result<String> delete(@RequestParam String ids) {
        log.info("删除菜品：{}", ids);
        // 将字符串id转换为List<Long>
        List<Long> idList = java.util.Arrays.stream(ids.split(","))
                .map(Long::valueOf)
                .collect(java.util.stream.Collectors.toList());
        return dishService.deleteDish(idList);
    }

    /**
     * 修改菜品状态
     */
    @PostMapping("/status/{status}")
    public Result<String> updateStatus(@PathVariable Integer status, @RequestParam String ids) {
        log.info("修改菜品状态，status={}, ids={}", status, ids);
        // 将字符串id转换为List<Long>
        List<Long> idList = java.util.Arrays.stream(ids.split(","))
                .map(Long::valueOf)
                .collect(java.util.stream.Collectors.toList());
        return dishService.updateStatus(status, idList);
    }

    /**
     * 查询所有在售菜品
     */
    @GetMapping("/list")
    public Result<List<Dish>> list(Dish dish) {
        log.info("查询所有在售菜品");
        return dishService.listDish(dish);
    }
}
